package com.bridgeintelligent.tag.bulkload.service.es.index;

import com.bridgeintelligent.tag.bulkload.api.DimInfo;
import com.bridgeintelligent.tag.bulkload.service.core.cache.CacheConfig;
import com.bridgeintelligent.tag.bulkload.service.core.cache.CacheUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by JackCheung on 2019/9/16 7:49 PM.
 */
public class MappingHelper {

    public static final String TYPE = "onedata_cust_view";

    /**
     * 创建映射类型
     *
     * @return
     */
    public static Map<String, Object> createMapping(String indexType) {
        Map<String, Object> fieldMap = new HashMap<>();
        Map<String, DimInfo> cacheMap = CacheUtils.getCacheMap(CacheConfig.DIM_INFO);
        cacheMap.forEach((k, v) -> {
            Map<String, String> typeMap = new HashMap<>();
            if (v.getDimType().equals(DimInfo.DimType.COMMON_DIM)) {
                typeMap.put("type", "keyword");
            } else if (DimInfo.DimType.FREE_DIM.equals(v.getDimType())) {
                typeMap.put("type", "double");
            }//指标类标签暂时注释
            else {
                typeMap.put("type", "keyword");
            }
            fieldMap.put(k, typeMap);
        });
        Map<String, Object> propertiesMap = new HashMap<>();
        propertiesMap.put("properties", fieldMap);
        Map<String, Object> doc = new HashMap<>();
        doc.put(indexType, propertiesMap);
        return doc;
    }

    /**
     * 创建字段映射类型
     *
     * @param response
     * @return
     */
    public static Map<String, Object> updateMapping(Map<String, String> response) {
        Map<String, Object> fieldMap = new HashMap<>();
        response.forEach((k, v) -> {
            Map<String, String> typeMap = new HashMap<>();
            typeMap.put("type", v);
            fieldMap.put(k, typeMap);
        });
        Map<String, Object> propertiesMap = new HashMap<>();
        propertiesMap.put("properties", fieldMap);
        return propertiesMap;
    }
}
